package com.qfmy.project.openApi.controller;

import com.qfmy.common.pojo.TokenVo;
import com.qfmy.common.utils.CommonUtils;
import com.qfmy.common.utils.RedisUtils;
import com.qfmy.common.utils.StringUtils;
import com.qfmy.core.base.service.TokenService;
import com.qfmy.core.constant.CacheConstants;
import com.qfmy.core.pojo.entity.ErrorCode;
import com.qfmy.core.pojo.exception.CustomException;
import com.qfmy.project.openApi.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述:
 *
 * @author zhengjt
 * @date 2022-10-27 16:09:05
 */
@Slf4j
@RestController
public class LoginController extends BaseOpenApiController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private TokenService tokenService;

    /**
     * 登录接口
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public Object login(HttpServletRequest request,
                        HttpServletResponse response,
                        String username,
                        String password,
                        String verCode,
                        String verCodeKey
    ) throws CustomException {

        //  判断参数是否都存在
        if (StringUtils.isNull(username)
                || StringUtils.isNull(password)
                || StringUtils.isNull(verCode)
                || StringUtils.isNull(verCodeKey)
        ){
            throw new CustomException(ErrorCode.NULL_PARAM);
        }

        //  获取redis中的验证码参数信息
        if (!redisUtils.hasKey(verCodeKey)){
            throw new CustomException("验证码已过期，请重新获取验证码！");
        }
        String redisVerCode = String.valueOf(redisUtils.get(verCodeKey));
        if (!redisVerCode.equals(verCode)){
            throw new CustomException("验证码错误，请重新输入！");
        }
        //  登录成功后即使清理该验证码，防止重复利用
        redisUtils.delete(verCodeKey);

        //  获取用户信息
        TokenVo object = loginService.loginUser(username, password);

        String token = tokenService.createToken(object);

        return token;
    }

    /**
     * 获取验证码
     * @return
     */
    @GetMapping("/getVerCode")
    public Object getVerCode(){

        //  验证码
        String verCode = CommonUtils.getVerCodeMixed(4);
        log.info(" 获取验证码 ： {}", verCode);
        //  redis的key
        String redisKey = CacheConstants.CACHE_HEARD_CODE + CommonUtils.getUUID();

        //  生成验证码图片

        //  转换成base64编码

        //  存入redis，过期时间为3分钟
        redisUtils.set(redisKey, verCode, 3*60);

        Map<String, String> result = new HashMap<>(2);
        //  验证码redis的key
        result.put("verCodeKey", redisKey);
        //  验证码图片
        result.put("verCodeImg", "");

        return result;
    }

    @RequestMapping("openTest")
    @ResponseBody
    public Object testOpenController(HttpServletRequest request, HttpServletResponse response) throws CustomException {
        log.info("========= 成功继承父接口 =========");
//        throw new CustomException("测试异常结果");
        return "测试成功，成功继承父类接口！";
    }
}
